from re import search, I
import inspect
from functools import wraps


# 正则提取key
class RegexDict:

    def __init__(self, n_dict, regex):
        self.n_dict = n_dict
        self.regex = regex

    def get(self):
        matches = {}
        for key in self.n_dict:
            # I - 模式忽略大小写匹配
            if search(self.regex, key, I):
                matches.update({key.replace(self.regex, ""): self.n_dict[key]})
        return matches


class NDict(dict):

    def __getitem__(self, item):
        if item not in self.keys():
            return None
        return super().__getitem__(item)

    def pop(self, k):
        return super().pop(k, None)

    def popitem(self):
        if not self:
            return None
        return super().popitem()


def wrapper(func):
    @wraps(func)
    def params(*args, **kw):
        py = inspect.stack()[1][1].split('\\')[-1]
        fc = inspect.stack()[1][3]
        r = func(*args, **kw)
        print(f"\n\t\033[1;33m******** Function: {func.__name__} ********\033[0m\n"
              f"\t调用方: <文件：{py} => function: {fc}>\n"
              f"\t声明参数: <{inspect.signature(func)}>\n"
              f"\t可变参数：<{args}>\n"
              f"\t关键字参数：<{kw}>\n"
              f"\t返回结果: {r}\n")
        return r

    return params


def contains_key(d: dict, key: (str, tuple)) -> bool:
    if isinstance(key, str):
        return key in d
    if isinstance(key, tuple):
        for k in key:
            if k in d:
                return True
        return False
    raise ValueError("key类型不是str或tuple")
